package com.mooc.fuxi.ui.news;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.flyco.tablayout.SegmentTabLayout;
import com.mooc.fuxi.R;
import com.mooc.fuxi.entity.BannerEntity;
import com.mooc.fuxi.entity.NewsEntity;
import com.mooc.fuxi.entity.TypeEntity;
import com.mooc.fuxi.mvvm.BaseActivity;
import com.mooc.fuxi.ui.detail.NedwsDetailActivity;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class NewsActivity extends BaseActivity implements
        OnRefreshLoadMoreListener, OnItemClickListener {

    private NewsViewModel viewModel = new NewsViewModel();
    private SegmentTabLayout news_tab;
    private SmartRefreshLayout news_refresh;
    private RecyclerView news_rv;
    private View headView;
    private Banner<String,NewsBannerAdapter> banner;
    private NewsAdapter adapter;
    private int page = 1;
    @Override
    public int bindLayout() {
        return R.layout.activity_news;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        initData();
    }

    private void init(){
        news_tab = f(R.id.news_tab);
        news_refresh = f(R.id.news_refresh);
        news_rv = f(R.id.news_rv);
        headView = LayoutInflater.from(this).inflate(R.layout.layout_banner,null,false);
        banner = headView.findViewById(R.id.news_banner);
        news_refresh.setRefreshHeader(new ClassicsHeader(this));
        news_refresh.setRefreshFooter(new ClassicsFooter(this));
        news_refresh.setOnRefreshLoadMoreListener(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        news_rv.setLayoutManager(manager);
        news_rv.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL));
        adapter = new NewsAdapter();
        adapter.setOnItemClickListener(this);
        adapter.addHeaderView(headView);
        news_rv.setAdapter(adapter);
    }

    private void initData(){
        viewModel.requestBanner().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BannerEntity>() {
                    @Override
                    public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.annotations.NonNull BannerEntity bannerEntity) {
                        List<String>list = new ArrayList<>();
                        for (BannerEntity.Values values : bannerEntity.data){
                            list.add("http:"+values.image);//由于banner中得图片地址
                            //无http:需要手动拼接http:
                        }
                        banner.setOrientation(Banner.HORIZONTAL)
                                .setAdapter(new NewsBannerAdapter(list))
                                .start();
                    }

                    @Override
                    public void onError(@io.reactivex.annotations.NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

        viewModel.requestType().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<TypeEntity>() {
                    @Override
                    public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.annotations.NonNull TypeEntity typeEntity) {
                        String[]titles = new String[typeEntity.data.size()];
                        for (int i =0;i < typeEntity.data.size();i ++){
                            titles[i]=typeEntity.data.get(i).typename;
                        }
                        news_tab.setTabData(titles);
                    }

                    @Override
                    public void onError(@io.reactivex.annotations.NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
        viewModel.requestNews(page).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NewsEntity>() {
                    @Override
                    public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.annotations.NonNull NewsEntity newsEntity) {
                        adapter.setNewInstance(newsEntity.data);
                    }

                    @Override
                    public void onError(@io.reactivex.annotations.NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        page += 1;
        viewModel.requestNews(page).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NewsEntity>() {
                    @Override
                    public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.annotations.NonNull NewsEntity newsEntity) {
                        adapter.addData(newsEntity.data);
                    }

                    @Override
                    public void onError(@io.reactivex.annotations.NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        page = 1;
        viewModel.requestNews(page).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NewsEntity>() {
                    @Override
                    public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.annotations.NonNull NewsEntity newsEntity) {
                        adapter.setNewInstance(newsEntity.data);
                    }

                    @Override
                    public void onError(@io.reactivex.annotations.NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        Intent intent = new Intent(this, NedwsDetailActivity.class);
        NewsEntity.Values values = this.adapter.getItem(position);
        intent.putExtra("news",values);
        startActivity(intent);

    }
}
